<template>
  <div class="home-user-w">
    <div class="back-info" @click="handBack">
      <img src="@/assets/back.png" alt="" />
      <span>返回</span>
    </div>
    <div class="home-user">
      <div class="info">
        <div
          :class="{ 'cursor-pointer': userInfo.id === user.userId }"
          @click="handUserHome"
        >
          <n-avatar round :size="100" :src="user.avatar" />
        </div>
        <div class="con">
          <div class="names">
            <div class="name">{{ user.nickname }}</div>
            <!-- <div class="tag">标签1</div>
            <div class="tag">标签2</div> -->
          </div>
          <div class="city">
            <span v-if="user.sex === 0">男</span>
            <span v-else-if="user.sex === 1">女</span>
            <span v-else>保密</span>
            <span class="dian"></span>
            <span v-if="user.birthday">{{ user.birthday }}</span>
          </div>
          <div class="desc">{{ user.remark }}</div>
          <div class="more flex">
            <div class="item">
              <span>作品</span>
              <span class="num">{{ user.photo_num_count }}</span>
            </div>
            <div class="item">
              <span>粉丝</span>
              <span class="num">{{ user.fans_count }}</span>
            </div>
            <div class="item">
              <span>关注</span>
              <span class="num">{{ user.concern_count }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="btns flex">
        <n-button class="btn" v-if="userInfo.id === user.id" @click="handEdit">
          <i class="landscape landscape-bianji"></i>
          <span>编辑</span>
        </n-button>
        <n-button
          v-if="userInfo.id !== user.id"
          class="btn"
          type="primary"
          @click="handFollow"
        >
          <template v-if="user.isFollow">
            <span>取消关注</span>
          </template>
          <template v-else>
            <i class="landscape landscape-jia"></i>
            <span>关注TA</span>
          </template>
        </n-button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { NAvatar, NButton } from 'naive-ui'
import followApi from '@/service/api/follow'
import { useAppsStore } from '@/stores/modules/app'
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'
import { userLoading } from '@/hooks/userLoading'
type PropsState = {
  user: any
}
const props = withDefaults(defineProps<PropsState>(), {
  user: () => {
    return {
      id: 0
    }
  }
})
const emit = defineEmits(['followChange'])
const handEdit = () => {
  router.push('/user/info')
}
const useStore = useAppsStore()
const { setLoginShow } = useStore
const { getUserInfo: userInfo } = storeToRefs(useStore)
const router: any = useRouter()
const handBack = () => {
  router.go(-1)
}
const handUserHome = () => {
  if (props.user.userId === userInfo.value.id) {
    router.push('/user/home')
  }
}
const { requestLoading, starLoading, stopLoading } = userLoading()
const handFollow = async () => {
  if (!userInfo.value?.id) {
    setLoginShow(true)
    return
  }
  if (props.user.id === userInfo.value.id) {
    msgUtil.$message.error('不能关注自己')
    return
  }
  if (requestLoading.value) return
  starLoading()
  try {
    if (props.user.isFollow) {
      const { code, msg } = await followApi.cancel({
        userid: props.user.id
      })
      if (code !== 0) {
        msgUtil.$message.error(msg)
        return
      }
      msgUtil.$message.success('取关成功')
    } else {
      const { code, msg } = await followApi.add({
        userid: props.user.id
      })
      if (code !== 0) {
        msgUtil.$message.error(msg)
        return
      }
      msgUtil.$message.success('关注成功')
    }
    emit('followChange')
  } finally {
    stopLoading()
  }
}
</script>

<style scoped lang="scss">
.home-user-w {
  margin-top: 34px;
  position: relative;
  .back-info {
    position: absolute;
    padding: 0px 0 0 38px;
    cursor: pointer;
    display: flex;
    align-items: center;
    img {
      width: 18px;
      height: 15px;
    }
    span {
      margin-left: 7px;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #999999;
    }
  }
}
.home-user {
  padding-top: 60px;
  display: flex;
  align-items: center;
  width: 1200px;
  margin: 0 auto 0 auto;
  padding-bottom: 37px;
  .info {
    flex: 1;
    display: flex;
    align-items: center;
    .con {
      margin-left: 24px;
      .names {
        display: flex;
        align-items: center;
        .name {
          font-size: 24px;
          font-family: PingFang SC, PingFang SC;
          font-weight: bold;
          color: #333333;
        }
        .tag {
          margin-left: 8px;
          font-size: 12px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #333333;
          padding: 4px 8px;
          background: #f5f5f5;
          border-radius: 4px;
        }
      }
      .city {
        margin-top: 8px;
        font-size: 14px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #999999;
        display: flex;
        align-items: center;
        .dian {
          margin: 0 8px;
          width: 4px;
          height: 4px;
          border-radius: 50%;
          background: #999;
        }
      }
      .desc {
        margin-top: 8px;
        font-size: 14px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #666666;
      }
      .more {
        margin-top: 12px;
        .item {
          padding: 0 29px;
          position: relative;
          &:first-child {
            padding-left: 0;
          }
          &:not(:last-child) {
            &:before {
              content: '';
              position: absolute;
              right: 0;
              top: 50%;
              transform: translateY(-50%);
              width: 0;
              height: 10px;
              border: 1px solid #eeeeee;
            }
          }
          span {
            font-size: 14px;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            color: #999999;

            &:last-child {
              margin-left: 8px;
              font-size: 24px;
              font-family: HONOR Sans, HONOR Sans;
              font-weight: 400;
              color: #333333;
            }
          }
        }
      }
    }
  }
  .btns {
    gap: 0 12px;
    .btn {
      font-size: 14px;
      width: 124px;
      .landscape {
        margin-right: 5px;
      }
    }
  }
}
</style>
